#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n,i,q;
    string chs;
    cin >> n;
    cin >> chs;

    for(i=1;i<=n;i++)
    {
        cin >> q;

        if(q==1)
        {
            string str;
            cin >> str;
            chs=chs+str;
            cout << chs << endl;
        }

        if(q==2)
        {
            int a1,b1;
            cin >> a1 >> b1;
            chs=chs.substr(a1,b1);
            cout << chs << endl;
        }

        if(q==3)
        {
            int a2;
            string str2;
            cin >> a2 >> str2;
            chs.insert(a2,str2);
            cout << chs << endl;
        }
        
        if(q==4)
        {
            string str3;
            cin >> str3;

            if(chs.find(str3) < chs.size())
            {
                cout << chs.find(str3) << endl;
            }
            else
            {
                cout << -1 << endl;
            }
        }
    }

    return 0;
}

